package belf.migrate.core.util;

import org.apache.log4j.PropertyConfigurator;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class LoadLogger {
    public static void initLog() {
        InputStream inputStream = null;
        try {
            Properties properties = new Properties();
            String logfile = ConfigUtil.getConfigDir() + File.separator + "log4j.properties";
            File file = new File(logfile);
            if(file.exists()){
                inputStream = new FileInputStream(file);
                System.out.println(String.format("load log4j.properties from %s.", logfile));
            }else{
                inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.properties");
                System.out.println("load log4j.properties from classpath.");
            }
            properties.load(inputStream);
            PropertyConfigurator.configure(properties);
            System.out.println("load log4j.properties successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
